package com.suarte.webapp.action;

import com.suarte.core.Employee;
import com.suarte.core.QuotationStatus;
import com.suarte.core.Remission;
import com.suarte.core.RemissionDetail;
import com.suarte.core.WorkOrder;
import com.suarte.core.WorkOrderDetail;
import com.suarte.core.WorkOrderStatus;
import com.suarte.core.service.EmployeeManager;
import com.suarte.core.service.QuotationManager;
import com.suarte.core.service.RemissionManager;
import com.suarte.core.service.WorkOrderManager;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.appfuse.model.User;

/**
 * @date   Dec 21, 2010
 * @author Ggutierrez
 */
public class WorkOrderForm extends BasePage implements Serializable {

    private WorkOrderManager workOrderManager;
    private RemissionManager remissionManager;
    private EmployeeManager employeeManager;
    private QuotationManager quotationManager;
    private WorkOrder workOrder = new WorkOrder();
    private Long id;
    private Employee responsible;

    public void setWorkOrderManager(WorkOrderManager workOrderManager) {
        this.workOrderManager = workOrderManager;
    }

    public void setRemissionManager(RemissionManager remissionManager) {
        this.remissionManager = remissionManager;
    }

    public void setEmployeeManager(EmployeeManager employeeManager) {
        this.employeeManager = employeeManager;
    }

    public void setQuotationManager(QuotationManager quotationManager) {
        this.quotationManager = quotationManager;
    }

    public WorkOrder getWorkOrder() {
        return workOrder;
    }

    public void setWorkOrder(WorkOrder workOrder) {
        this.workOrder = workOrder;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        workOrderManager.remove(workOrder.getId());
        addMessage("contact.deleted");

        return "list";
    }

    public String edit() {
        if (id != null) {
            workOrder = workOrderManager.get(id);
        } else {
            workOrder = new WorkOrder();
        }

        return "edit";
    }

    public String add() {
        workOrder = new WorkOrder();

        return "add";
    }

    public String save() {
        boolean isNew = (workOrder.getId() == null);
        workOrderManager.save(workOrder);

        String key = (isNew) ? "workOrder.added" : "workOrder.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }

    public String generateRemission() {
        HttpServletRequest request = getRequest();
        Date date = new Date();
        User requestUser = userManager.getUserByUsername(request.getRemoteUser());

        Remission remission = new Remission();
        remission.setWorkOrder(workOrder);
        remission.setDescription(workOrder.getDescription());
        remission.setDate(date);
        remission.setEmployee(null);
        remission.setCreatedBy(requestUser);
        remission.setModifiedBy(requestUser);
        remission.setCreatedOn(date);
        remission.setModifiedOn(date);

        for (WorkOrderDetail qDetail : workOrder.getDetails()) {
            RemissionDetail remissionDetail = new RemissionDetail();
            remissionDetail.setQuotationDetail(qDetail.getQuotationDetail());
            remission.addDetail(remissionDetail);
            qDetail.setStatus(WorkOrderStatus.REMITIDA);
        }

        remissionManager.save(remission);

        workOrder.setOutputDate(date);
        workOrder.setModifiedOn(date);
        workOrder.setModifiedBy(requestUser);
        workOrder.setStatus(WorkOrderStatus.REMITIDA);
        workOrderManager.save(workOrder);
        if (workOrder.getQuotation().getStatus().equals(QuotationStatus.APROBADA)) {
            workOrder.getQuotation().setStatus(QuotationStatus.REMITIDA);
            workOrder.getQuotation().setDeliveryStatus(QuotationStatus.REMITIDA);
            workOrder.getQuotation().setModifiedOn(date);
            workOrder.getQuotation().setModifiedBy(requestUser);
            quotationManager.save(workOrder.getQuotation());
        } else {
            workOrder.getQuotation().setDeliveryStatus(QuotationStatus.REMITIDA);
            workOrder.getQuotation().setModifiedOn(date);
            workOrder.getQuotation().setModifiedBy(requestUser);
            quotationManager.save(workOrder.getQuotation());
        }
        String key = "workOrder.remission";
        addMessage(key);

        return "list";
    }

    public List<Employee> getResponsibles() {
        return employeeManager.getAll();
    }
}
